﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.HtmlControls;

namespace feedBackWP.InputForm
{
    [ToolboxItemAttribute(false)]
    public class InputForm : WebPart
    {

        ////////////// global vars ///////////////////
        // Private variables
        private String _FeedbackListName;
        SPList Mylist;
        //HTML Controls Vars
        TextBox Comments, f_Title;
        HtmlGenericControl InputFormDiv;

        //The initialization code
        public InputForm()
        {
            _FeedbackListName = "setup";
        }

        //the property block
        #region InputForm list Property
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [Category("Setup")]
        [WebDisplayName("Feedback List Info")]
        [Description("Enter the name of the list this webpart will use to save Feedbacks")]
        [DefaultValue("setup")]
        public string FeedbackListName
        {
            get
            {
                return _FeedbackListName;
            }
            set
            {
                _FeedbackListName = value;
            }
        }
        #endregion

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            //The goal of this web part is to enable user to enter data using a HTML form. When user submits the 
            //form, we will take the data and insert it into our Sharepoint List.
            //Since the name of the Sharepoint List can change in future, we will declare it as a property. 
            //The WebPart Property is something that can be setup externally through web part by the user.

            //Outer Division
            HtmlGenericControl OuterDiv = new HtmlGenericControl("div");
            OuterDiv.ID = "Outerdiv1";
            OuterDiv.Attributes["style"] = "width:800px; text-align:left; padding:12px; font-size:12px";

            //Add H2 -- It will be centered and will say "Feedback Form"
            HtmlGenericControl topHeading = new HtmlGenericControl("h2");
            topHeading.Attributes["style"] = "text-align:center; font-size:15px";
            topHeading.InnerText = "Feedback Form";
            OuterDiv.Controls.Add(topHeading);


            //Add p -- Small Description Paragraph
            HtmlGenericControl topDesc = new HtmlGenericControl("p");
            topDesc.Attributes["style"] = "text-align:center; font-size:13px; font-color:#000";
            topDesc.InnerText = "This form can be used to submit comments. These comments " +
                "will be saved in a SP list. Please remember to set List Name in this " +
                "webpart settings";
            OuterDiv.Controls.Add(topDesc);


            //Show warning if feedback list's name is not set in the properties
            //check if list name is set
            if (this.FeedbackListName == "setup")
            {
                Label warningLabel = new Label();
                warningLabel.Text = "Please specify List name in Web Part Properties";
                warningLabel.Attributes["style"] = "font-weight:bold; color:red;";
                OuterDiv.Controls.Add(warningLabel);
            }
            else
            {
                //if the name is set, show the form
                //Add Input Form to the Division
                OuterDiv.Controls.Add(DisplayForm());

            }
          

            //Add the outer division to the page 
            this.Controls.Add(OuterDiv);
  
        }

        ////////////////////Functions Block//////////////////
        
        //This function returns the input form Table enclosed in a Division
        private HtmlGenericControl DisplayForm()
        {
            Table t = new Table();

            //Textbox for Feedback Title
            f_Title = new TextBox();
            f_Title.Attributes["style"] = "width: 350px;";
            f_Title.ID = "txt_title";

            //Add Title textbox to the table
            t.Controls.Add(GetRow("Feedback Title: ",f_Title));
            
            //Textbox for Feedback Comments
            Comments = new TextBox();
            Comments.ID = "txt_Comments";
            Comments.Attributes["style"] = "width: 350px;";
            Comments.TextMode = TextBoxMode.MultiLine;
            Comments.Height = 50;
            
            //Add Comments Textbox to the table
            t.Controls.Add(GetRow("Comments: ", Comments));

            //Button for submitting the form
            Button SubmitBtn = new Button();
            SubmitBtn.ID = "btn_Submit";
            SubmitBtn.Click += new EventHandler(SubmitBtn_Click);
            SubmitBtn.Text = "Submit Form";
            //Add button to the table
            t.Controls.Add(GetRow("", SubmitBtn));

            InputFormDiv = new HtmlGenericControl("div");

            InputFormDiv.Controls.Add(t);

            return InputFormDiv;
        }

        //This functions returns a table row.
        //we will use it to add rows to our input form table
        protected TableRow GetRow(String LabelText, System.Web.UI.Control ControlToAdd)
        {
            TableRow tr;
            TableCell tc, tc2;
            tr = new TableRow();

            tc = new TableCell();
            tc.VerticalAlign = VerticalAlign.Top;
            tc.Width = 133;
            HtmlGenericControl OuterDiv = new HtmlGenericControl("div");
            OuterDiv.Attributes["style"] = "";
            OuterDiv.InnerHtml = LabelText;
            tc.Controls.Add(OuterDiv);

            tc2 = new TableCell();
            tc2.VerticalAlign = VerticalAlign.Top;
            tc2.Controls.Add(ControlToAdd);

            tr.Controls.Add(tc);
            tr.Controls.Add(tc2);

            return tr;
        }

        //This is event handler for our submit button
        protected void SubmitBtn_Click(object sender, EventArgs e)
        {
            //Make sure textboxes are not empty
            if (f_Title.Text != string.Empty && Comments.Text != string.Empty)
            {
                Mylist = SPContext.Current.Web.Lists[this.FeedbackListName];

                SPListItem li = Mylist.Items.Add();
                
                //WARNING!!! These columns (Title and comments) should exist in your Sharepoint list!
                li["Title"] = f_Title.Text;
                li["comments"] = Comments.Text;

                //save changes
                li.Update();

                //Hide the input form
                InputFormDiv.Visible = false;

                //Show confirmation message
                HtmlGenericControl ConfMsg = new HtmlGenericControl("p");
                ConfMsg.Attributes["style"] = "text-align:center; font-size:13px; font-color:#000; font-weight:bold";
                ConfMsg.InnerHtml = "Thank you. Your comments are saved. Please <a href=\"" + 
                    Context.Request.Url.GetLeftPart(UriPartial.Path).ToString() + "\">click here to go back</a>. ";
                this.Controls.Add(ConfMsg);
            }

        }

         

    }
}
